/*
 * Copyright 2025 The JA-SIG Collaborative. All rights reserved.
 * distributed with this file and available online at
 */
package com.lap.auth.adapter.driving.persistence.menu.repository;

import com.lap.auth.adapter.driving.persistence.menu.MenuViewLogConverter;
import com.lap.auth.adapter.driving.persistence.menu.mapper.MenuViewLogMapper;
import com.lap.auth.adapter.driving.persistence.menu.po.MenuViewLogPo;
import com.lap.auth.domain.menu.MenuViewLog;
import com.lap.auth.domain.menu.repository.MenuViewLogRepository;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

@RequiredArgsConstructor
@Repository
public class MenuViewLogRepositoryImpl implements MenuViewLogRepository {

  private final MenuViewLogConverter menuViewLogConverter;
  private final MenuViewLogMapper menuViewLogMapper;

  @Override
  public Integer save(MenuViewLog menuViewLog) {
    MenuViewLogPo menuViewLogPo = menuViewLogConverter.toPo(menuViewLog);
    if (menuViewLog.isNew()) {
      menuViewLogMapper.insert(menuViewLogPo);
      return menuViewLogPo.getId();
    }
    return menuViewLogMapper.updateById(menuViewLogPo);
  }

  @Override
  public int deleteById(Integer id) {
    return menuViewLogMapper.deleteById(id);
  }

  @Override
  public Optional<MenuViewLog> findById(Integer id) {
    var menuViewLogPo = menuViewLogMapper.selectById(id);
    return Optional.ofNullable(menuViewLogConverter.toDomain(menuViewLogPo));
  }
}
